package com.chinemit.cui.components.navigation.tab
{
	
	import com.chinemit.cui.components.core.CUIComponent;
	import com.chinemit.cui.events.SelectEvent;
	
	import flash.events.MouseEvent;
	
	import spark.components.mediaClasses.VolumeBar;

	public class IconTabBar extends CUIComponent
	{
		public static const LAYOUT_VERTICAL:String ="vertical";
		public static const LAYOUT_HORIZONTAL:String ="horizontal";
		private var _currentIndex:int;
		private var _numItem:int;
		private var itemRenderClass:Class;
		
		private var _gap:int = 5;
		
		private var _layout:String = "horizontal";
		
		private var data:Array;
		
		private var isDynamicPos:Boolean = false;
		
		public function get layout():String
		{
			return _layout;
		}
		
		public function set layout(value:String):void
		{
			_layout = value;
			init(data,currentIndex)
		}
		
		/**
		 * Tab Item Gap 默认间隔为5 
		 * @return 
		 * 
		 */		
		public function get gap():int
		{
			return _gap;
		}
		
		public function set gap(value:int):void
		{
			_gap = value;
		}
		
		public function get numItem():int
		{
			return _numItem;
		}
		
		public function set numItem(value:int):void
		{
			_numItem = value;
		}
		
		public function get currentIndex():int
		{
			return _currentIndex;
		}
		
		public function set currentIndex(value:int):void
		{
			_currentIndex = value;
			
			reset();
			//发送SELECT事件
			if(data){
				this.dispatchEvent(new SelectEvent(SelectEvent.EVENT_SELECT,false,false,_currentIndex,data[_currentIndex]));
			}
		}
		
		
		public function IconTabBar(_itemRenderer:Class,dynamic:Boolean = false)
		{
			super();
			
			if(_itemRenderer == null){
				return;
			}
			itemRenderClass = _itemRenderer;
			
			isDynamicPos = dynamic;
			
			this.addEventListener(SelectEvent.EVENT_SIZE_CHANGE,selecEventHandler);
		}
		
		private function selecEventHandler(event:SelectEvent):void{
			dynamicPos();
		}
		
				
		override protected function into():void{
			return;
			super.into();
		}
				
		override protected function out():void{
			return;
			super.out();
		}
		
		/**
		 * 设置 
		 * @param sum
		 * @param index
		 * @param _data
		 * 
		 */		
		public function init(_data:Array,_index:int = 0):void{
			
			if(!itemRenderClass){
				return;
			}
			//
			if(!_data){
				return;
			}
			data = _data;
			
			this.removeAllChild();
			
			numItem = data.length;
			
			var btn:*;
			var i:int= 0;
			if(this.layout == LAYOUT_HORIZONTAL){
				
				for(i=0;i<numItem;i++){
					btn = new itemRenderClass(data[i].text,data[i].icon);
					this.addChild(btn);
					btn.x = i*(btn.width+gap);
					
					if(i==currentIndex){
						btn.setFocus();
					}
					btn.index = i;
					btn.addEventListener(MouseEvent.CLICK,selectHandler);
				}
				
			}else{
				
				for(i=0;i<numItem;i++){
					btn = new itemRenderClass(data[i].text,data[i].icon);
					this.addChild(btn);
					btn.y = i*(btn.height+gap);
					
					if(i==currentIndex){
						btn.setFocus();
					}
					btn.index = i;
					btn.addEventListener(MouseEvent.CLICK,selectHandler);
				}
			}
			//设置起始索引
			currentIndex = _index;
			//
			this.dispatchEvent(new SelectEvent(SelectEvent.EVENT_SIZE_CHANGE));
			
			
		}
		
		/**
		 * 重新设置 
		 * 
		 */		
		private function reset():void{
			
			var btn:*;
			for(var i:int= 0 ;i<numItem;i++){
				
				if(this.getChildAt(i) is itemRenderClass){
					btn = this.getChildAt(i) as itemRenderClass;
					btn.reset();
				}
				
				if(i==currentIndex){
					btn.setFocus();
				}
			}
			
			dynamicPos();
		}
		
		private function selectHandler(event:MouseEvent):void{
			
			var btn:TabBarItem; 
			btn = event.target as TabBarItem;
			
			if(btn.index == this.currentIndex){
				return;
			}
			
			for(var i:int= 0 ;i<numItem;i++){
				btn = this.getChildAt(i) as TabBarItem;
				btn.reset();
			}
			btn = event.target as TabBarItem;
			btn.setFocus();
			currentIndex = btn.index;
			
			dynamicPos();
		}
		
		private function dynamicPos():void{
			
			if(!isDynamicPos){
				return;
			}
			var i:int= 0;
			if(this.layout == LAYOUT_HORIZONTAL){
				var _x:Number = 0;
				for(i=0;i<this.numChildren;i++){
					this.getChildAt(i).x = _x;
					_x+=this.getChildAt(i).width;
				}
			}else{
				var _y:Number = 0;
				for(i=0;i<this.numChildren;i++){
					this.getChildAt(i).y = _y;
					_y+=this.getChildAt(i).height;
				}
			}
			
		}
	}
}